<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Selforderps extends Base
{
    /**
     * 异地登录验证：last
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
//--------------------------------此控制器乃统购专用！！！！！！！！！！--------------------------------------------
    /**
     * 我的某一个订单详情
     */
    public function myTongOrderInfo()
    {
        return json(['status' => 0, 'msg' => '请升级APP版本']);
        extract(input());
        $order = Db::table('up_self_order')
            ->where('order_id',$order_id)
            ->find();
        $where = [
            'od.oc_order_id' => $order_id
        ];
        if(isset($tui) && $tui == 1){
            $where['od.oc_refund_status'] = 0; //未售后的，售后结束的不能再次售后
            if ($order['admin_tui'] != 1 && $order['admin_tui'] != 4) {
                return json(['status' => 0, 'msg' => '此订单已结束，不允许售后']);
            }
        }
        $field = 'num,unit_price,goods_info,good_id,good_name,good_thumb,attr_id,th_status,refund_status,detail_id,detail_express,detail_express_no';
        $field = 'oc_id,oc_num,oc_price,oc_size,oc_goods_info,oc_name,oc_image,oc_th_status,oc_refund_status,oc_detail_express,oc_detail_express_no';
        $field .= ',status';
        $details = Db::table('up_self_order_contract')->alias('od')
            ->join("up_self_order_tui t",'od.oc_id=t.order_oc_id','LEFT')
            ->where($where)
            ->field($field)
            ->select();
//
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
//
        $wldata = [];
        if(count($details) <= 1){
            $wldata = orderWuLiu($details[0]['oc_detail_express_no']);
            if($wldata['status'] == 200){
                $wldata['time'] = $wldata['msg'][0]['time'];
                $wldata['msg'] = $wldata['msg'][0]['context'];
            }
        }
        return json(['status'=>200,'msg'=>$order,'goods'=>$details,'count_num' => count($details),'sondata' => $sondata,'wldata' => $wldata]);
    }
    /**
     * 我的某一个订单里的所有的商品（为评价之前调用）
     */
    public function myTongAllInfo()
    {
        extract(input());
        $where = [
            'order_id' => $order_id
        ];
        $field = 'num,unit_price,goods_info,good_id,good_name,good_thumb,attr_id,th_status,refund_status,detail_id,detail_express,detail_express_no';
        $details = Db::table('up_self_order_detail')->alias('od')
            ->join("up_goods g",'od.goods_id=g.good_id','LEFT')
            ->where($where)
            ->field($field)
            ->select();
        return json(['status'=>200,'msg'=>$details,'count_num' => count($details)]);
    }
    /**
     * msg：订单快递公司和单号、缩略图
     * wldata：
     *      status为200时展示快递信息，其他值时不展示，只展示里面的msg即可
     *      msg为物流信息数组
     *      wlstate为快递单当前状态，0在途，1揽收，2疑难，3签收，4退签，5派件，6退回
     */
    public function getWlInfo()
    {
        extract(input());
        $data = Db::table('up_self_order_contract')
            ->where('oc_id',$oc_id)
            ->field('oc_detail_express,oc_detail_express_no,oc_image')
            ->find();
        $wldata = orderWuLiu($data['oc_detail_express_no']);
        return json(['status'=>200,'msg'=>$data,'wldata'=>$wldata]);
    }
    /**
     * 申请换货时可以不可以现场调换
     * 1可以 0不可以
     */
    public function canLiveChange()
    {
        extract(input());
        $can = $this->getSonByUser($user_id);
        if($can['status'] == 200){
            $live = Db::table('up_school')->where('school_id',$can['msg']['uson_school'])->value('school_change');
            if($live == 2){
                return json(['status'=>200,'msg'=>1]);
            }
        }
        return json(['status'=>200,'msg'=>0]);
    }
    /**
     * 申请售后：只有已发货后的才可以
     * 后台说明：
     *          1、如果订单1件商品：同意或拒绝则tui_status已完成
     *          2、如为多件，sale_status为1时同意或拒绝则tui_status已完成
     */
    public function afterSaleTongOrder()
    {
        return json(['status' => 0, 'msg' => '请升级APP版本']);
        extract(input());
        $today = date('Y-m-d H:i:s');
        $erp_send = Db::table('up_self_order')->where('order_id', $order_id)->field('is_send,order_erp')->find();
        if ($erp_send['is_send'] < 2) {
            return json(['status' => 0, 'msg' => '只有已收货订单才可以申请售后']);
        }
        if ($erp_send['order_erp'] != 1) {
            return json(['status' => 0, 'msg' => '此统购订单不能售后']);
        }
        $oc_ids = trim($oc_ids,',');
        if(!$oc_ids){
            return json(['status' => 0, 'msg' => '请选择套装']);
        }
        $oc_ids = explode(',', $oc_ids);
        //
        $can = $this->getSonByUser($user_id);
        if($can['status'] != 200){
            return json($can);
        }
        $sondata = $can['msg'];
        $tui_user_son_id = $sondata['uson_id'];
        //
        Db::startTrans();
        try {
            foreach ($oc_ids as $k => $v) {
                //1、入售后表：
                $number = time() . rand(100000, 999999);
                $tui = [
                    'user_id' => $user_id,
                    'tui_type' => 3,
                    'tui_user_son_id' => $tui_user_son_id,
                    'order_id' => $order_id,
                    'order_oc_id' => $v, //和下面的order_detail_id，这俩一样的值，指的是套装id
                    'order_detail_id' => $v, //
                    'order_no' => $number,
                    'type' => $type,//1换货 2退款 3现场调换
                    'refund_price' => 0,
                    'remark' => $remark,
                    'express' => $express,
                    'express_no' => $express_no,
                    'created_at' => $today
                ];
                if($type == 2){ //如果是退款的话退多少
                    $suan = Db::table('up_self_order_contract')->where('oc_id', $v)->value('oc_price');
                    $tui['refund_price'] = $suan;
                }
                Db::table('up_self_order_tui')->insert($tui);
            }
            //2-1、改套装表
            Db::table('up_self_order_contract')->where('oc_id', 'in',$oc_ids)->update([
                'oc_refund_status' => 1,
                'oc_th_status' => $type,
            ]);
            //2-2、改详情表
            Db::table('up_self_order_detail')->where('oc_id_erp', 'in',$oc_ids)->update([
                'refund_status' => 1,
                'th_status' => $type,
            ]);
            //3、改order表售后状态
            $goodsnum = Db::table('up_self_order_detail')->where('order_id', $order_id)->count();
            if($goodsnum > 1){ //超出一件：全售后则order为售后中，否则不改
                $goods_errornum = Db::table('up_self_order_detail')
                    ->where('order_id', $order_id)
                    ->where('th_status', '>',0)
                    ->count();
                if($goodsnum == $goods_errornum){
                    Db::table('up_self_order')->where('order_id', $order_id)->update([
                        'sale_status' => 1,
                        'tui_status' => 0,
                    ]);
                }
            }else{ //只一件：detail售后状态order售后状态
                Db::table('up_self_order')->where('order_id', $order_id)->update([
                    'sale_status' => $type+1,
                    'tui_status' => 0,
                ]);
            }
            Db::commit();
            try{
                $this->Jpush(1, $user_id, '发起售后通知', ['n_id' => $order_id,'type' => 3,'n_type' => 3]);
            }catch(\Exception $e){

            }
            $title = '退换货通知';
            $content = '发起退换货后，我们将在收货后进行核实，并为您处理退换货事宜。';
            allInNotice($title,$content,3,$user_id,$order_id,3);
            return json(['status' => 200, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status' => 0, 'msg' => '操作失败，请刷新重试']);
        }
    }
    /**
     * 查看退货套装的详情，想看套装内的商品，调套装下的衣服接口
     * @return \think\response\Json
     */
    public function getTaoTuiInfo()
    {
        extract(input());
        $field = 'oc_id,oc_num,oc_price,oc_goods_info,oc_size,oc_name,oc_image,oc_th_status,oc_refund_status';
        $field .= ',remark,merchant_remark,t.created_at,order_no,status,express,express_no,merchant_express,merchant_express_no';
        $data = Db::table('up_self_order_tui')->alias('t')
            ->join('up_self_order_contract od','t.order_oc_id=od.oc_id','LEFT')
            ->where('order_oc_id', $oc_id)
            ->order('created_at desc')
            ->field($field)
            ->find();
        return json(['status' => 200, 'msg' =>$data]);
    }
}